package cn.wolfcode.web.controller;


import cn.wolfcode.domain.JsonRequest;
import cn.wolfcode.domain.Notice;
import cn.wolfcode.enums.NoticeEnums;
import cn.wolfcode.qo.NoticeQueryObject;
import cn.wolfcode.service.IEmpNoticeService;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;

    @Autowired
    private IEmpNoticeService empNoticeService;


//    @RequiredPermission(name="公告管理页面",expression="notice:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") NoticeQueryObject qo){
//        不是管理员时
        if (!UserContext.getUser().isAdmin()){
            qo.setSt(0);
        }
        qo.setId(UserContext.getUser().getId());
        PageInfo<Notice> pageInfo = noticeService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("stas", NoticeEnums.values());
//        右上角显示
//        model.addAttribute("username",UserContext.getUser().getName());
//        定义一个变量来获取未读的数量
        int i=empNoticeService.selectUnRead(UserContext.getUser().getId(),false);
        model.addAttribute("isum",i);
//        获取右上角分页的title,时间
        List<Notice> minus= noticeService.selectMinute(UserContext.getUser().getId(),false);
        model.addAttribute("ns",minus);
        return "notice/list";
    }


    @RequiredPermission(name="公告管理删除",expression="notice:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           noticeService.delete(id);
        }
        return "redirect:/notice/list";
    }


    @RequiredPermission(name="公告管理新增/编辑",expression="notice:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Notice notice) {
        if (notice.getId() == null) {
            noticeService.save(notice);
        } else {
            noticeService.update(notice);
        }
        return "redirect:/notice/list";
    }
    @RequiredPermission(name="公告管理新增/编辑",expression="notice:saveOrUpdate")
    @RequestMapping("/updatepack")
    public String updatepack(Long id) {
        noticeService.updatepack(id);
        return "redirect:/notice/list";
    }


    @RequiredPermission(name="公告管理新增/编辑",expression="notice:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("notice",noticeService.get(id));
        }
        return "notice/input";
    }

    @RequestMapping("/show")
    public String show(Long id,Model model){
//       传回公告的id 修改中间表的状态状态
        empNoticeService.updateStatu(id,UserContext.getUser().getId());
//        共享公告的内容
        model.addAttribute("notice",noticeService.get(id));
        return "notice/show";
    }

    @RequestMapping("/power")
    @ResponseBody
    public JsonRequest getAdmin(){
        JsonRequest jsonRequest = new JsonRequest();
        if (!UserContext.getUser().isAdmin()){
            jsonRequest.setSuccess(false);
            jsonRequest.setMsg("您无权操作!");
        }
        return jsonRequest;
    }

    @RequestMapping("/statu")
    @ResponseBody
    public JsonRequest getStatu(Long id){
        JsonRequest jsonRequest = new JsonRequest();
        Notice notice = noticeService.get(id);
            if (notice.getStatus()){
                jsonRequest.setSuccess(false);
                jsonRequest.setMsg("已发布过,请重新添加!");
           }
        return jsonRequest;
    }
}
